package com.yituo.mjworld.mvc.dao;

import com.yituo.mjworld.core.utils.BaseDao;
import com.yituo.mjworld.mvc.entity.web.WxUserinfo;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信用户判断
 */
@Repository
public class WxUserDao extends BaseDao {

  /**
   * 该微信用户是否已经被绑定.
   *
   * @param openId
   * @return
   */
  public boolean isExitsByOpenId(String openId) {
    String hql = "from WxUserinfo where openid=:openid";
    Map<String, Object> param = new HashMap<String, Object>(2);
    param.put("openid", openId);
    List<WxUserinfo> list = find(hql, param);
    if (list != null && list.size() > 0) {
      return true;
    }
    return false;
  }

  /**
   * 某某用户是否为微信用户
   *
   * @param userId
   * @return
   */
  public boolean isExitsByUserId(String userId) {
    String hql = "from WxUserinfo where userId=:userId";
    Map<String, Object> param = new HashMap<String, Object>(2);
    param.put("userId", userId);
    List<WxUserinfo> list = find(hql, param);
    if (list != null && list.size() > 0) {
      return true;
    }
    return false;
  }

  /**
   *
   * @return
   */
  public WxUserinfo getWxUserByOpenId(String openid){
    String hql = "from WxUserinfo where openid=:openid";
    Map<String, Object> param = new HashMap<String, Object>(2);
    param.put("openid", openid);
    List<WxUserinfo> list = find(hql, param);
    if (list != null && list.size() > 0) {
      return list.get(0);
    }
    return null;
  }
}
